var areaCache = {}, defaultParent = "0";
var entname = $("#entname"), buslicenseno = $("#buslicenseno"), leader = $("#leader"), contactnumber = $("#contactnumber");
var provinceid = $("#provinceid"), cityid = $("#cityid"), districtid = $("#districtid"), streetid = $("#streetid"), detailaddr = $("#detailaddr");
var buslicense = $("#buslicense");
var buslicenseSelector = $("#buslicenseSelector");

$(function() {
	$("input[prop='fileInput']").change(onFileInputChanged);
	$("input[prop='fileSelector']").click(onFileSelectorClick);
	$("input[prop='areaSelector']").click(onAreaSelectorClick);
	
	queryAreas();
});

//文件输入框变化事件
function onFileInputChanged(e) {
	var file = e.currentTarget.files[0], fileid = e.currentTarget.id;
	if (!/^.+\.(jpg)|(jpeg)|(png)$/.test(file.name)) {
		mobileGlobal.message("请选择图片文件");
		return;
	}
	
	switch (fileid) {
		case "buslicense":
			buslicenseSelector.val(file.name);
			break;
	}
}

//文件选择框点击事件
function onFileSelectorClick() {
	var selectorId = $(this).attr("id");
	switch (selectorId) {
		case "buslicenseSelector":
			buslicense.click();
			break;
	}
}

//区域选择框点击事件
function onAreaSelectorClick() {
	var selector = $(this), selectorId = selector.attr("id"), selectorTitle = null, defaultValue = null, selectorData = null;
	
	var pvid = null, valueid = null;
	if (selectorId.startsWith("provinceid")) {
		valueid = "provinceid";
		selectorTitle = "请选择省份";
	} else if (selectorId.startsWith("cityid")) {
		pvid = "provinceid";
		valueid = "cityid";
		selectorTitle = "请选择城市";
	} else if (selectorId.startsWith("districtid")) {
		pvid = "cityid";
		valueid = "districtid";
		selectorTitle = "请选择区县";
	} else if (selectorId.startsWith("streetid")) {
		pvid = "districtid";
		valueid = "streetid";
		selectorTitle = "请选择街道";
	} else return;
	
	var parent = null;
	if (pvid == null) parent = defaultParent;
	else parent = $("#" + pvid).val();
	if (!parent) {
		mobileGlobal.message("请先选择上级区域");
		return;
	}
	
	defaultValue = $("#" + valueid).val();
	selectorData = areaCache[parent];
	if (!selectorData) {
		queryAreas(parent, selectorId, selectorTitle, defaultValue);
		return;
	}
	
	openAreaSelector(selectorId, selectorTitle, defaultValue, selectorData);
}

//查询区域
function queryAreas(parent, selectorId, selectorTitle, defaultValue) {
	var areaKey = defaultParent, data = null;
	if (parent && parent != defaultParent) {
		areaKey = parent;
		data = {parent : parent};
	}
	
	mobileGlobal.sendRequest(__ctx + "/nr/getAreasForSelect.xhtml", data, null, function(data, status) {
		mobileGlobal.hideLoading();
		if (data.status) {
			areaCache[areaKey] = data.result;
			if (selectorId) openAreaSelector(selectorId, selectorTitle, defaultValue, data.result);
		}
	});
}

//打开区域选择器
function openAreaSelector(id, title, value, data) {
	if (!data || !Array.isArray(data) || data.length == 0) {
		mobileGlobal.message("未找到任何区域");
		return;
	}
	
	var defaultValue = (value) ? [value] : [data[0].value];
	weui.picker(data, {
		id : id, 
		title : title, 
		defaultValue : defaultValue, 
		onConfirm : function(result) {
			onAreaSelected(this.id, result[0]);
		}
	});
}

//区域选择事件
function onAreaSelected(selectorId, selectedObject) {
	var selector = $("#" + selectorId);
	
	var valueid = null, children = null;
	if (selectorId.startsWith("provinceid")) {
		valueid = "provinceid";
		children = ["cityid", "districtid", "streetid"];
	} else if (selectorId.startsWith("cityid")) {
		valueid = "cityid";
		children = ["districtid", "streetid"];
	} else if (selectorId.startsWith("districtid")) {
		valueid = "districtid";
		children = ["streetid"];
	} else if (selectorId.startsWith("streetid")) {
		valueid = "streetid";
	} else return;
	
	var valuer = $("#" + valueid);
	if (valuer.val() != selectedObject.value) {
		valuer.val(selectedObject.value);
		selector.val(selectedObject.label);
		
		if (children == null) return;
		
		for (var index = 0; index < children.length; index ++) {
			var child = children[index];
			$("#" + child).val("");
			$("#" + child + "Selector").val("");
		}
		
		if (!areaCache[selectedObject.value]) queryAreas(selectedObject.value);
	}
}

//提交按钮点击事件
function onSubmitClick() {
	var formData = new FormData();
	
	//基本信息
	formData.append("entname", entname.val());
	formData.append("buslicenseno", buslicenseno.val());
	if (buslicense[0].files[0]) formData.append("buslicense", buslicense[0].files[0]);
	formData.append("leader", leader.val());
	formData.append("contactnumber", contactnumber.val());
	formData.append("provinceid", provinceid.val());
	formData.append("cityid", cityid.val());
	formData.append("districtid", districtid.val());
	formData.append("streetid", streetid.val());
	formData.append("detailaddr", detailaddr.val());
	
	if (!formData.get("entname")) {
		mobileGlobal.message("网吧名称必须填写");
		return;
	}
	if (!formData.get("buslicenseno")) {
		mobileGlobal.message("营业执照号必须填写");
		return;
	}
	if (!formData.get("buslicense")) {
		mobileGlobal.message("营业执照必须上传");
		return;
	}
	if (!formData.get("leader")) {
		mobileGlobal.message("负责人必须填写");
		return;
	}
	if (!/^1[3456789]\d{9}$/.test(formData.get("contactnumber"))) {
		mobileGlobal.message("联系电话必须填写且为有效手机号");
		return;
	}
	if (!formData.get("streetid")) {
		mobileGlobal.message("所在区域必须填写");
		return;
	}
	if (!formData.get("detailaddr")) {
		mobileGlobal.message("详细地址必须填写");
		return;
	}
	
	mobileGlobal.loading("正在提交数据，请稍后...");
	
	$.ajax({
		url : __ctx + "/nr/registerCybercafe.xhtml", 
		data : formData, 
		contentType : false, 
		processData : false, 
		success : function(data, status) {
			mobileGlobal.hideLoading();
			if (data.status) {
				mobileGlobal.openMessageDialog("注册成功，即将前往登录页面！", function() {
					mobileGlobal.closeMessageDialog();
					mobileGlobal.forward(__mobilectx + "/login.xhtml");
				});
			} else {
				mobileGlobal.openMessageDialog(data.result, function() {
					mobileGlobal.closeMessageDialog();
				});
			}
		}, 
		error : function(xhr, msg, e) {
			mobileGlobal.hideLoading();
			mobileGlobal.openMessageDialog("网络请求错误！", function() {
				mobileGlobal.closeMessageDialog();
			});
		}
	});
}

//返回按钮点击事件
function onBackClick() {
	mobileGlobal.forward(__mobilectx + "/login.xhtml");
}
